package com.cskaoyan.service.impl;

import com.cskaoyan.bean.CskaoyanmallStorage;
import com.cskaoyan.bean.CskaoyanmallStorageExample;
import com.cskaoyan.bean.vo.StorageListVo;
import com.cskaoyan.mapper.CskaoyanmallStorageMapper;
import com.cskaoyan.service.AdminStorageService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Fantasylkh
 */
@Service
public class AdminStorageServiceImpl implements AdminStorageService {

    @Autowired
    CskaoyanmallStorageMapper storageMapper;

    @Override
    public StorageListVo storageList(int page, int limit, String key, String name, String sort, String order) {
        CskaoyanmallStorageExample storageExample = new CskaoyanmallStorageExample();
        storageExample.setOrderByClause(sort + " " + order);
        CskaoyanmallStorageExample.Criteria criteria = storageExample.createCriteria();
        if (name != null && !"".equals(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (key != null && !"".equals(key)) {
            criteria.andKeyEqualTo(key);
        }
        List<CskaoyanmallStorage> storageList = storageMapper.selectByExample(storageExample);
        PageInfo<CskaoyanmallStorage> pageInfo = new PageInfo<>(storageList);
        long total = pageInfo.getTotal();
        StorageListVo storageListVo = new StorageListVo();
        storageListVo.setItems(storageList);
        storageListVo.setTotal(total);
        return storageListVo;
    }

    @Override
    public CskaoyanmallStorage storageUpdate(CskaoyanmallStorage storage) {

        int i = storageMapper.updateByPrimaryKey(storage);
        if (i != 0) {
            return storage;
        }
        return null;

    }

    @Override
    public Integer storageDelete(CskaoyanmallStorage storage) {
        Integer i = storageMapper.deleteByPrimaryKey(storage.getId());
        return i;
    }
}
